﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using MReviewerClient.Models;
using MReviewerClient.Providers;
using System.Collections.ObjectModel;
using MReviewerClient.MReviewerSvc;
using System.ComponentModel;
using System.Windows.Data;

namespace MReviewerClient.Views
{
    public partial class ListEstablishmentView : PhoneApplicationPage
    {
        #region Fields

        /// <summary>
        /// The list of establishment
        /// </summary>
        public ObservableCollection<EstablishmentDTO> Establishments { get; set; }

        /// <summary>
        /// Using CollectionViewSource for sorting or filter
        /// </summary>
        private CollectionViewSource listingDataView;

        /// <summary>
        /// Checking whether Page is loaded
        /// </summary>
        private bool IsPageLoaded = false;

        #endregion

        #region Constructor

        /// <summary>
        /// The default constructor
        /// </summary>
        public ListEstablishmentView()
        {
            InitializeComponent();

            this.Establishments = EstablishmentModel.Instance.Establishments;
            listingDataView = this.Resources["listingDataView"] as CollectionViewSource;
            this.DataContext = this;
        }

        #endregion

        #region Methods & Handlers

        /// <summary>
        /// Searching function
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            listingDataView.Filter += new FilterEventHandler(FilterHandler);
        }

        /// <summary>
        /// The handler for filter data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FilterHandler(object sender, FilterEventArgs e)
        {
            EstablishmentDTO establishment = e.Item as EstablishmentDTO;
            if (establishment != null)
            {
                if (establishment.Name.ToUpper().Contains(txtEstablishmentName.Text.ToUpper()))
                    e.Accepted = true;
                else
                    e.Accepted = false;
            }
        }

        private void menSort_Click(object sender, EventArgs e)
        {
            listingDataView.SortDescriptions.Add(
                new SortDescription("Name", ListSortDirection.Ascending));
        }

        private void menNotSort_Click(object sender, EventArgs e)
        {
            listingDataView.SortDescriptions.Clear();
        }

        private void menCafe_Click(object sender, EventArgs e)
        {
            listingDataView.Filter += new FilterEventHandler(FilterCafeHandler);
        }

        /// <summary>
        /// The handler for filter only cafe
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FilterCafeHandler(object sender, FilterEventArgs e)
        {
            EstablishmentDTO establishment = e.Item as EstablishmentDTO;
            if (establishment != null)
            {
                if (establishment.EstablishmentType.Name.Equals("Cafe"))
                    e.Accepted = true;
                else
                    e.Accepted = false;
            }
        }

        private void menBar_Click(object sender, EventArgs e)
        {
            listingDataView.Filter += new FilterEventHandler(FilterBarHandler);
        }

        /// <summary>
        /// The handler for filter only bar
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FilterBarHandler(object sender, FilterEventArgs e)
        {
            EstablishmentDTO establishment = e.Item as EstablishmentDTO;
            if (establishment != null)
            {
                if (establishment.EstablishmentType.Name.Equals("Bar"))
                    e.Accepted = true;
                else
                    e.Accepted = false;
            }
        }

        private void menRestaurant_Click(object sender, EventArgs e)
        {
            listingDataView.Filter += new FilterEventHandler(FilterRestaurantHandler);
        }

        /// <summary>
        /// The handler for filter only restaurant
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FilterRestaurantHandler(object sender, FilterEventArgs e)
        {
            EstablishmentDTO establishment = e.Item as EstablishmentDTO;
            if (establishment != null)
            {
                if (establishment.EstablishmentType.Name.Equals("Restaurant"))
                    e.Accepted = true;
                else
                    e.Accepted = false;
            }
        }

        private void menListAll_Click(object sender, EventArgs e)
        {
            listingDataView.Filter += new FilterEventHandler(SelectAllHandler);
        }

        /// <summary>
        /// The handler for filter data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SelectAllHandler(object sender, FilterEventArgs e)
        {
            EstablishmentDTO establishment = e.Item as EstablishmentDTO;
            if (establishment != null)
                e.Accepted = true;
        }

        private void lstEstablishment_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (IsPageLoaded == false)
                return;
            if (lstEstablishment.SelectedItem != null)
                NavigationService.Navigate(new Uri(string.Format("/Views/DetailEstablishmentView.xaml?id={0}", 
                    (lstEstablishment.SelectedItem as EstablishmentDTO).ID), UriKind.RelativeOrAbsolute));
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.lstEstablishment.SelectedIndex = -1;
            IsPageLoaded = true;
        }

        #endregion
    }
}